package com.lanou3g.demo.controller;

import com.lanou3g.demo.controller.params.RoleParams;
import com.lanou3g.demo.entity.RoleInfo;
import com.lanou3g.demo.service.IRoleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
public class RoleInfoController {

    @Autowired
    private IRoleInfoService roleInfoService;

    /**
     * 角色管理	角色浏览	查看所有角色
     *
     * @return
     */
    @RequestMapping("/role.do")
    public List<RoleInfo> findAllWithRole() {
        return roleInfoService.findAllWithRole();
    }

    /**
     * 角色管理 增加角色	增加新角色
     *
     * @param data
     * @param req
     * @return
     */
    @RequestMapping(value = "/addRole.do", method = RequestMethod.POST)
    public boolean addRoleWithModule(@RequestBody List<Integer> data, HttpServletRequest req) {
        try {
            String rName = req.getParameter("rName");
            if (rName != null && rName.trim().length() > 0) {
                RoleParams roleParams = new RoleParams();
                roleParams.setrName(rName);
                roleParams.setRoleModuleIds(data);
                int i = roleInfoService.addRoleWithRoleModule(roleParams);
                return i > 0;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 修改 回显
     */
    @RequestMapping(value = "/modify.do", method = RequestMethod.GET)
    public RoleParams modify(HttpServletRequest req, RoleInfo roleInfo) {
        String roleId = req.getParameter("roleId");
        if (roleId != null) {
            Integer rId = Integer.parseInt(roleId);
            roleInfo.setRoleId(rId);
            RoleParams roleParams = roleInfoService.selectRoleModule(roleInfo);
            return roleParams;
        } else {
            return null;
        }
    }

    /**
     * 修改角色:在保存之前,获取到回显信息,将其删除,保存时将新数据插入
     *
     * @return
     */
    @RequestMapping("/modifyRole.do")
    public boolean modifyRole(HttpServletRequest req, RoleInfo roleInfo) {
        RoleInfo ri = roleInfoService.selectRoleId(roleInfo);
        roleInfo = ri;
        boolean b = deleteRole(roleInfo);
        if (b) {
            List<Integer> data = new ArrayList<>();
            addRoleWithModule(data, req);
            return true;
        } else {
            return false;
        }
    }


    /**
     * 删除角色
     *
     * @param roleInfo
     * @return
     */
    @RequestMapping("/deleteRole.do")
    public boolean deleteRole(RoleInfo roleInfo) {
        int i = roleInfoService.deleteRoleWithRoleModule(roleInfo);
        return i > 0;
    }
}
